package me.alide.modules.survey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.alide.common.exception.RRException;
import me.alide.modules.survey.dao.SurveyEntityDao;
import me.alide.modules.survey.entity.MySurvey;
import me.alide.modules.survey.entity.SurveyEntity;
import me.alide.modules.survey.form.StatusForm;
import me.alide.modules.survey.form.SurveyDetail;
import me.alide.modules.survey.service.SurveyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Anzhen
 * @description:
 * @PROJECT_NAME: alide
 * @create: 2021-07-25 14:32
 **/
@Service
public class SurveyServiceImpl implements SurveyService {

    @Resource
    private SurveyEntityDao surveyEntityDao;

    @Override
    public int save(SurveyEntity surveyEntity) {
        return surveyEntityDao.insert(surveyEntity);
    }

    @Override
    public int delete(Long[] ids) {

        LambdaQueryWrapper<SurveyEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(SurveyEntity::getId, ids);
        return surveyEntityDao.delete(wrapper);

    }

    @Override
    public int update(SurveyEntity surveyEntity) {
        return surveyEntityDao.updateById(surveyEntity);
    }

    @Override
    public List<SurveyEntity> queryAll() {
        return surveyEntityDao.selectList(Wrappers.lambdaQuery());
    }

    @Override
    public SurveyEntity queryById(Long id) {
        return surveyEntityDao.selectById(id);
    }

    @Override
    public List<MySurvey> queryBySurveyUserId(Long userId, int page, int pageSize) {
        IPage<MySurvey> pageObj = new Page<>(page, pageSize);
        return surveyEntityDao.queryBySurveyUserId(userId, pageObj);
    }

    @Override
    public List<MySurvey> queryBySurveyList(int page, int pageSize) {
        IPage<MySurvey> pageObj = new Page<>(page, pageSize);
        return surveyEntityDao.queryBySurveyList(pageObj);
    }

    @Override
    public void updateStatus(StatusForm status) {
        SurveyEntity exist = queryById(status.getSurveyId());
        if(exist != null) {
            exist.setArchiveStatus(status.getArchiveStatus());
            exist.setQuestionStatus(status.getQuestionStatus());
            exist.setImpliedStatus(status.getImpliedStatus());
            this.update(exist);
        }else {
            throw new RRException("走访不存在");
        }
    }


    @Override
    public List<SurveyDetail> queryList(String startTime, String endTime) {
        return surveyEntityDao.queryList(startTime, endTime);
    }
}
